$(function () {
    //提交按钮点击事件
    $("#submitBtn").click(function () {
        //获得文本框的值
        var brokername=$("#brokername").val();
        var decs=$("#decs").val();
        var brokerid=$("#brokerid").val();
        var seatid=$("#seatid").val();
        var dealtype=$("#dealtype").val();
        var dealSite=$("#dealSite").val();
        var brokeragerate=$("#brokeragerate").val();

        //判断是否存在Id,有Id调用修改的方法，没Id调用添加
        if (brokerid.length==0){//添加
            $.ajax({
                url:"seat/add",
                type:"post",

                data:{"brokername":brokername,"decs":decs,"dealtype":dealtype,"dealSite":dealSite,"brokeragerate":brokeragerate},
                success:function (data) {
                    layer.msg(data);
                    window.location.reload();
                }
            })
        }else{//修改
            $.ajax({
                url:"seat/updata",
                type:"post",
                data:{"brokername":brokername,"decs":decs,"dealtype":dealtype,"dealSite":dealSite,"brokeragerate":brokeragerate,"seatid":seatid,"brokerid":brokerid},
                success:function (data) {
                    layer.msg(data);
                    //刷新页面
                    window.location.reload();
                }
            })
        }
    });



    /*-------------表字段数据开始----------------*/
    layui.use('table', function() {
        var table = layui.table;
        table.render({
            elem: '#test'
            , url: '/seat/selectAll'//控制器中条件查询的分页列表
            , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            , defaultToolbar: ['filter', 'exports', 'print']
            , title: '管理人表'
            , cols: [[
                {type: 'checkbox', fixed: 'left'}
                , {type: 'numbers', title: '序号'}
                , {field: 'brokerid', title: '券商ID'}
                , {field: 'seatid', title: '席位id'}
                , {field: 'brokername', title: '券商名'}
                , {field: 'seatname', title: '名字'}
                , {field: 'dealSite', title: '地点'}
                , {field: 'dealtype', title: '类型'}
                , {field: 'brokeragerate', title: '利率'}
            ]]
            //开启分页
            , page: true
            , id:"dataTable"
        });
        /*------------------表字段结束---------------*/

        /*--------------------模糊查询开始----------------------*/
        //模糊查询
        $("#seekBtn").click(function () {
            //获得输入框的值
            var d=$("#demoReload").val()
            table.reload("dataTable", {
                url: "/seat/selectLike"
                , where: {
                    condition:d
                }
            });
        });
        /*----------------------模糊查询结束----------------------------*/

        //头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            var layer = layui.layer;
            switch(obj.event){
                //编辑框
                case 'getCheckData':
                    var data = checkStatus.data;
                    if(data.length >1){
                        layer.msg("一次只能修改一个");
                        break;
                    }else if(data.length< 1){
                        layer.msg("请选择一条需要修改的数据");
                        break;
                    }else{
                        layer.open({
                            type:1,
                            title:'修改管理人',
                            content:$("#saveOrUpdateDiv"),
                            area:['800px','600px'],
                        })
                        $("#brokerid").val(data[0].brokerid);
                        $("#seatid").val(data[0].seatid);
                        $("#brokername").val(data[0].brokername);
                        $("#decs").val(data[0].decs);
                        $("#dealtype").val(data[0].dealtype);
                        $("#dealSite").val(data[0].dealSite);
                        $("#seatname").val(data[0].seatname);
                        $("#brokeragerate").val(data[0].brokeragerate);
                        break;
                    }
                //console.log(data[0])
                //添加页面
                case 'add':
                    var data = checkStatus.data;
                    layer.open({
                        type:1,
                        title:'添加管理人',
                        content:$("#saveOrUpdateDiv"),
                        area:['800px','600px'],
                    });
                    //清空文本框的值
                    $(".layui-inline input[type=text]").val("")
                    break;

                //删除
                case 'delete':
                    //获得复选框所选数组
                    var check =table.checkStatus("dataTable"),data=checkStatus.data;
                    if(data==""){
                        layer.msg("至少选择一行进行操作！！！",{icon:2});
                        return;
                        break;
                    }
                    //被选中的
                    var ids ="";
                    if(data.length >0){
                        for(var i=0; i<data.length; i++){
                            ids +=data[i].brokerid+",";
                            console.log(ids);
                        }
                    }
                    layer.confirm("确定要删除当前选中的数据吗?"+ids,function (index) {
                        $.ajax({
                            type:'post',
                            url:'broker/delete',
                            data:{"ids":ids.substr(0,ids.length-1)},
                            success:function(data){
                                layer.msg("删除成功",{icon:1});
                                window.location.reload();
                            },
                            error:function(data){
                                alert("失败");
                            }
                        });
                    });
                    break;
                //自定义头工具栏右侧图标 - 提示
                case 'LAYTABLE_TIPS':
                    layer.alert('这是工具栏右侧自定义的一个图标按钮');
                    break;
            };
        });
        /*-------------------头工具栏事件结束---------------------*/

        //监听行工具事件
        table.on('tool(test)', function(obj){
            var data = obj.data;
            //console.log(obj)
            if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    obj.del();
                    layer.close(index);
                });
            } else if(obj.event === 'edit'){
                layer.prompt({
                    formType: 2
                    ,value: data.email
                }, function(value, index){
                    obj.update({
                        email: value
                    });
                    layer.close(index);
                });
            }
        });
    });




});
